/**
 * 单位选择器
 */
var DepSelector = {
	/**
	 * @param callback　回调函数
	 * @param isSingle　是否单选
	 */
	getView : function(callback,isSingle, selval, depNames) {
		var depIds = selval;
		//单选
		if(typeof(callback) == 'object'){
			this.scope = callback.scope;
			this.callback = callback.callback;
		} else {
			this.scope = this;
			this.callback = callback;
		}
		
		if(isSingle==null || isSingle==undefined){
			isSingle=false;
		}
		
		// 左侧树
		this.treeDep = Ext.create('Ext.ux.TreePanelEditor', {
			title : '单位信息显示',
			region : 'west',
			width : 210,
			height : 300,
			split : true,
			collapsible : true,
			autoScroll : true,
			url : __ctxPath + '/system/listDepartment.do',
			root : {
				expanded : true
			},
			rootVisible : false,
			scope: this,
			onclick : this.clickNode
		});
		// 中间的表格
		this.gridPanel = new KE.GridPanel({
			title : '单位列表' + (isSingle?'':'(双击选中)'),
			autoScroll : true,
			region : 'center',
			isShowTbar: false,
			pageSize: 10000,
			showPaging: false,
			singleSelect: isSingle,
			autoWidth:false,
			rownumbercfg: {
				width: 30
			},
			url : __ctxPath + '/system/selectDepartment.do',
			fields : ['depId', 'depName','depth'],
			columns : [{
					header : 'depId',
					dataIndex : 'depId',
					hidden : true
				}, {
					header : "单位名称",
					dataIndex : 'depName',
					align: 'left',
					renderer:function(value,metadata,record){
						var str='';
						var level=record.data.depth;
						if(level!=null&& !isNaN(level)){
							for(var i=2;i<=level;i++){
								str+='<img src="' + __ctxPath+ '/images/system/down.gif"/>';
							}
						}
						str+=value;
						return str;
					},
					width : 60
				}]
			
		}); 
		// 双击事件
		var me = this;
		this.gridPanel.on('rowdblclick',function(grid, rec){
			var selGrid = me.selectedDepGrid;
			var selStore = selGrid.getStore();
			var rows = me.gridPanel.getSelectionModel().getSelection();
			for(var i= 0 ; i<rows.length ; i++){
				var depId = rows[i].data.depId;
				var depName = rows[i].data.depName;				
				var isExist = false;
				//查找是否存在该记录
				for(var j=0;j<selStore.getCount();j++){
					if(selStore.getAt(j).data.depId == depId){
						isExist = true;
						break;
					}
				}
				if(!isExist){
					var newData = {depId : depId,depName : depName};
					var rec = selStore.getModel().create(newData);
					selStore.insert(selStore.getCount(), rec);
				}
			}
		});
		// 已选单位
		this.selectedDepGrid = new KE.GridPanel({
			title : '已选单位(双击删除)',
			width : '100%',
			autoWidth : true,
			showPaging: false,
			isShowTbar: false,
			height : '100%',
			autoHeight : true,
			autoScroll : true,
			border : false,
			store : new Ext.data.ArrayStore({
    			fields : ['depId', 'depName']
			}),
			trackMouseOver : true,
			columns : [{
				header : "单位名称",
				dataIndex : 'depName'
			},{
				dataIndex : 'depId',
				hidden: true
			}]
		}); // end of this selectedUserGrid
		
		this.selectedDepGrid.on('rowdblclick',function(grid,e){
			var grid = me.selectedDepGrid;
			var store = grid.getStore();
			var rows = grid.getSelectionModel().getSelection();
			for(var i =0; i<rows.length; i++){
				store.remove(rows[i]);
			}
		});
		
		//多选添加的面板
		this.selectedPanel = new Ext.Panel({
			layout : 'border',
			region : 'east',
			width : 260,
			border : false,
			items : [new Ext.Panel({
				region : 'west',
				frame : true,
				width : 60,
				layout : {
                    type : 'vbox',
                    pack : 'center',
                    align : 'stretch'
                },
                defaults : {
                	xtype : 'button',
                	margin: '0 2 5 2'
                },
                items : [{
                	iconCls : 'add-all',
                	text : '',
                	scope : this,
                	handler : this.addAll
                },{
                	iconCls : 'rem-all',
                	text : '',
                	scope : this,
                	handler : this.removeAll
                }]
			}),{
				region : 'center',
				layout: 'fit',
				items : [this.selectedDepGrid]
			}]
		}); // selectedPanel
		
		var panel = new Ext.Panel({
			id : 'contactPanel',
			layout : 'border',
			border : false,
			anchor : '96%,96%',
			items : [this.treeDep, this.gridPanel]
		}); // end of this contactPanel
		//添加：多选面板
		if(isSingle != null && isSingle == false){
			panel.add(this.selectedPanel);
			panel.doLayout();
		}

		// 单选：根据传入值，选中GridPanel
		Ext.defer(function() {
			if(isSingle && selval) {
				var model = this.gridPanel.getSelectionModel();
				this.gridPanel.getStore().each(function(rec) {
					if(rec.data.depId == selval) {
						model.select([rec]);
						return;
					}
				});
			}
			if(!isSingle && depIds && depIds.length>0) {
				var selStore = this.selectedDepGrid.getStore();
				depIds = depIds.substring(1, depIds.length-1);
				var arrDepIds = depIds.split(',');
				var arrDepNames = depNames.split('，');
				for(var index=0;index<arrDepIds.length;index++){
					var newData = {depId: arrDepIds[index], depName: arrDepNames[index]};
					var rec = selStore.getModel().create(newData);
					selStore.insert(selStore.getCount(), rec);
				}
			}
		}, 200, this);
				
		this.win = new Ext.Window({
			//id : 'depSelectWindow',
			title : '单位选择器',
			iconCls : 'menu-department',
			width : 750,
			height : 480,
			border : false,
			layout : 'fit',
			items : [panel],
			modal : true,
			buttonAlign : 'center',
			maximizable : true,
			buttons : [{
				iconCls : 'btn-ok',
				text : '确定',
				scope : this,
				handler : function() {
					this.submit(this.callback, isSingle);
				}
			}, {
				text : '取消',
				iconCls : 'btn-cancel',
				handler : this.close,
				scope : this
			}]
		});
		return this.win;
	},
	
	/**
	 * 添加所有
	 */
	addAll : function(){
		var selGrid = this.selectedDepGrid;
		var selStore = selGrid.getStore();
		var rows = this.gridPanel.getSelectionModel().getSelection();
		for(var i = 0; i<rows.length; i++){
			var depId = rows[i].data.depId;
			var depName = rows[i].data.depName;			
			var isExist = false;
			//查找是否存在该记录
			for(var j=0; j<selStore.getCount(); j++){
				if(selStore.getAt(j).data.depId== depId){
					isExist = true;
					break;
				}
			}
			if(!isExist){
				var newData = {depId:depId,depName:depName};
				var rec = selStore.getModel().create(newData);
				selStore.insert(selStore.getCount(), rec);
			}
		}
		selGrid.getView().refresh();
	},
	
	/**
	 * 移除所有
	 */
	removeAll : function(){
		var selGrid = this.selectedDepGrid;
		var rows = selGrid.getSelectionModel().getSelection();
		var selStore = selGrid.getStore();
		for(var i=0 ;i<rows.length; i++){
			selStore.remove(rows[i]);
		}
	},
	
	/**
	 * 确定，提交
	 * @param isSingle 是否单选
	 * @param callback 回传函数
	 */
	submit : function(callback, isSingle){
		var depNames = '';		
		if(isSingle == null || isSingle == false){//选择多个
			var depIds = ','; //depIds前后加上逗号，方面like查询
			var selStore = this.selectedDepGrid.getStore();
			for(var i = 0 ; i<selStore.getCount(); i++){
				if (i > 0) {
					depIds += ',';
					depNames += '，';					
				}
				depIds += selStore.getAt(i).data.depId;
				depNames += selStore.getAt(i).data.depName;				
			}
			// depIds后增加1个逗号
			if(depIds != '') {
				depIds += ',';
			}
		} else {//选择单个
			var depIds = '';
			var grid = this.gridPanel;
			var rows = grid.getSelectionModel().getSelection();
			for (var i = 0; i < rows.length; i++) {
				if (i > 0) {
					depIds += ',';
					depNames += '，';					
				}
				depIds += rows[i].data.depId;
				depNames += rows[i].data.depName;				
			}
		}
		
		if (callback != null) {
//			if(isCharge){
//				callback.call(this, chargeIds, chargeNames);
//			}else{
				callback.call(this, depIds, depNames);
//			}
		}
		// Ext.getCmp('depSelectWindow').close();
		this.win.close();
	},
	
	/**
	 * 关闭当前窗口
	 */
	close : function(){
		// Ext.getCmp('depSelectWindow').close();
		this.win.close();
	},
	// 
	clickNode: function(view, node) {
		if (node != null) {
			var store = this.gridPanel.getStore();
			store.proxy.url = __ctxPath + '/system/selectDepartment.do';
			store.proxy.extraParams = {
				depId : node.id
			};
			store.load();
		}
	}		

};